<?php

set_time_limit(1000);

require dirname(__FILE__).'/../config.php';

$root_dir = '../';

include WP_VERSION_PATH . '/header.php';

echo '<h2>WP Version Fixed</h2>'."\n\n";

if( true === wp_version_upgrade_sqlite())
	echo '<p><strong>Done!</strong></p>';
else
	echo '<p>Failed!</p>';

include WP_VERSION_PATH . '/footer.php';

function wp_version_upgrade_sqlite()
{
	$pdo = new PDO('sqlite:'.SQLITE_PATH);
	$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

	try {
		$pdo->query("CREATE TABLE IF NOT EXISTS wp_types_temp ( 
			ID INTEGER PRIMARY KEY AUTOINCREMENT,
			info_type INTEGER(9),
			info_name TEXT(255),
			info_definition TEXT(1000),
			version_0_71 INTEGER(1),
			version_1_0_0 INTEGER(1),
			version_1_0_1 INTEGER(1),
			version_1_0_2 INTEGER(1),
			version_1_2_0 INTEGER(1),
			version_1_2_1 INTEGER(1),
			version_1_2_2 INTEGER(1),
			version_1_5_0 INTEGER(1),
			version_1_5_1 INTEGER(1),
			version_1_5_2 INTEGER(1),
			version_2_0_0 INTEGER(1),
			version_2_0_1 INTEGER(1),
			version_2_0_4 INTEGER(1),
			version_2_0_5 INTEGER(1),
			version_2_0_6 INTEGER(1),
			version_2_0_7 INTEGER(1),
			version_2_0_8 INTEGER(1),
			version_2_0_9 INTEGER(1),
			version_2_0_10 INTEGER(1),
			version_2_0_11 INTEGER(1),
			version_2_1_0 INTEGER(1),
			version_2_1_1 INTEGER(1),
			version_2_1_2 INTEGER(1),
			version_2_1_3 INTEGER(1),
			version_2_2_0 INTEGER(1),
			version_2_2_1 INTEGER(1),
			version_2_2_2 INTEGER(1),
			version_2_2_3 INTEGER(1),
			version_2_3_0 INTEGER(1),
			version_2_3_1 INTEGER(1),
			version_2_3_2 INTEGER(1),
			version_2_3_3 INTEGER(1),
			version_2_5_0 INTEGER(1),
			version_2_5_1 INTEGER(1),
			version_2_6_0 INTEGER(1)
		);
		CREATE UNIQUE INDEX IF NOT EXISTS unique_name ON wp_types_temp (info_type, info_name);
		CREATE INDEX IF NOT EXISTS info_name ON wp_types_temp (info_name);");
	} catch(PDOException $e) {
		echo $e->getMessage();
		return false;
	}

	try
	{
		$types = $pdo->query("SELECT * FROM wp_types")->fetchAll(PDO::FETCH_OBJ);
		$existPrepare = $pdo->prepare("SELECT ID FROM wp_types_temp WHERE info_type = ? AND info_name = ?");
		$namePrepare = $pdo->prepare("INSERT INTO wp_types_temp (info_type, info_name) VALUES (?, ?);");
	}
	catch(PDOException $e)
	{
		echo $e->getMessage();
		return false;
	}

	$save = array();
	foreach($types as $row)
	{
		if( !isset($save[ $row->info_type ]) )
		{
			$save[ $row->info_type ] = array();
		}	

		try
		{
			$existPrepare->execute(array($row->info_type, $row->info_name));
			$id = $existPrepare->fetch(PDO::FETCH_OBJ)->ID;
		} 
		catch(PDOException $e)
		{
			echo $e->getMessage();
			return false;
		}
		
		if( !isset($save[ $row->info_type ][ $row->info_name ]) && !is_numeric($id) )
		{
			$namePrepare->execute(array($row->info_type, $row->info_name));
			$save[ $row->info_type ][ $row->info_name ] = $pdo->lastInsertId();
		}
		else
		{
			$save[ $row->info_type ][ $row->info_name ] = $id;
		}

		$update = '';

		if( 1 == $row->version_0_71 )
		{
			$update .= " version_0_71 = 1";
		}

		if( 1 == $row->version_1_0_0 )
		{
			if( !empty($update) )
				$update .= ',';
			$update .= " version_1_0_0 = 1";
		}

		if( 1 == $row->version_1_0_1 )
		{
			if( !empty($update) )
				$update .= ',';
			$update .= " version_1_0_1 = 1";
		}

		if( 1 == $row->version_1_0_2 )
		{
			if( !empty($update) )
				$update .= ',';
			$update .= " version_1_0_2 = 1";
		}

		if( 1 == $row->version_1_2_0 )
		{
			if( !empty($update) )
				$update .= ',';
			$update .= " version_1_2_0 = 1";
		}

		if( 1 == $row->version_1_2_1 )
		{
			if( !empty($update) )
				$update .= ',';
			$update .= " version_1_2_1 = 1";
		}

		if( 1 == $row->version_1_2_2 )
		{
			if( !empty($update) )
				$update .= ',';
			$update .= " version_1_2_2 = 1";
		}

		if( 1 == $row->version_1_5_0 )
		{
			if( !empty($update) )
				$update .= ',';
			$update .= " version_1_5_0 = 1";
		}

		if( 1 == $row->version_1_5_1 )
		{
			if( !empty($update) )
				$update .= ',';
			$update .= " version_1_5_1 = 1";
		}

		if( 1 == $row->version_1_5_2 )
		{
			if( !empty($update) )
				$update .= ',';
			$update .= " version_1_5_2 = 1";
		}

		if( 1 == $row->version_2_0_0 )
		{
			if( !empty($update) )
				$update .= ',';
			$update .= " version_2_0_0 = 1";
		}

		if( 1 == $row->version_2_0_1 )
		{
			if( !empty($update) )
				$update .= ',';
			$update .= " version_2_0_1 = 1";
		}

		if( 1 == $row->version_2_0_4 )
		{
			if( !empty($update) )
				$update .= ',';
			$update .= " version_2_0_4 = 1";
		}

		if( 1 == $row->version_2_0_5 )
		{
			if( !empty($update) )
				$update .= ',';
			$update .= " version_2_0_5 = 1";
		}

		if( 1 == $row->version_2_0_6 )
		{
			if( !empty($update) )
				$update .= ',';
			$update .= " version_2_0_6 = 1";
		}

		if( 1 == $row->version_2_0_7 )
		{
			if( !empty($update) )
				$update .= ',';
			$update .= " version_2_0_7 = 1";
		}

		if( 1 == $row->version_2_0_8 )
		{
			if( !empty($update) )
				$update .= ',';
			$update .= " version_2_0_8 = 1";
		}

		if( 1 == $row->version_2_0_9 )
		{
			if( !empty($update) )
				$update .= ',';
			$update .= " version_2_0_9 = 1";
		}

		if( 1 == $row->version_2_0_10 )
		{
			if( !empty($update) )
				$update .= ',';
			$update .= " version_2_0_10 = 1";
		}

		if( 1 == $row->version_2_0_11 )
		{
			if( !empty($update) )
				$update .= ',';
			$update .= " version_2_0_11 = 1";
		}

		if( 1 == $row->version_2_1_0 )
		{
			if( !empty($update) )
				$update .= ',';
			$update .= " version_2_1_0 = 1";
		}

		if( 1 == $row->version_2_1_1 )
		{
			if( !empty($update) )
				$update .= ',';
			$update .= " version_2_1_1 = 1";
		}

		if( 1 == $row->version_2_1_2 )
		{
			if( !empty($update) )
				$update .= ',';
			$update .= " version_2_1_2 = 1";
		}

		if( 1 == $row->version_2_1_3 )
		{
			if( !empty($update) )
				$update .= ',';
			$update .= " version_2_1_3 = 1";
		}

		if( 1 == $row->version_2_2_0 )
		{
			if( !empty($update) )
				$update .= ',';
			$update .= " version_2_2_0 = 1";
		}

		if( 1 == $row->version_2_2_1 )
		{
			if( !empty($update) )
				$update .= ',';
			$update .= " version_2_2_1 = 1";
		}

		if( 1 == $row->version_2_2_2 )
		{
			if( !empty($update) )
				$update .= ',';
			$update .= " version_2_2_2 = 1";
		}

		if( 1 == $row->version_2_2_3 )
		{
			if( !empty($update) )
				$update .= ',';
			$update .= " version_2_2_3 = 1";
		}

		if( 1 == $row->version_2_3_0 )
		{
			if( !empty($update) )
				$update .= ',';
			$update .= " version_2_3_0 = 1";
		}

		if( 1 == $row->version_2_3_1 )
		{
			if( !empty($update) )
				$update .= ',';
			$update .= " version_2_3_1 = 1";
		}

		if( 1 == $row->version_2_3_2 )
		{
			if( !empty($update) )
				$update .= ',';
			$update .= " version_2_3_2 = 1";
		}

		if( 1 == $row->version_2_3_3 )
		{
			if( !empty($update) )
				$update .= ',';
			$update .= " version_2_3_3 = 1";
		}

		if( 1 == $row->version_2_5_0 )
		{
			if( !empty($update) )
				$update .= ',';
			$update .= " version_2_5_0 = 1";
		}

		if( 1 == $row->version_2_5_1 )
		{
			if( !empty($update) )
				$update .= ',';
			$update .= " version_2_5_1 = 1";
		}

		if( 1 == $row->version_2_6_0 )
		{
			if( !empty($update) )
				$update .= ',';
			$update .= " version_2_6_0 = 1";
		}

		if( !empty($update) )
		{
			$sql = 'UPDATE wp_types_temp SET' . $update ." WHERE info_type = ".$row->info_type." AND info_name = '".$row->info_name."'";
			var_dump($sql);
			$pdo->query($sql);
		}

	}

	$pdo->query("DROP TABLE wp_types");
	$pdo->query("ALTER TABLE wp_types_temp RENAME TO wp_types");
	$pdo->query("VACUUM");
	
	return true;
}